#pragma once
 
#include <mmsystem.h>   
#include "MutableBuffer.h"

#include "common\inc\thread\locksection.h"
#include "common\inc\thread\lock.h"
using fs::thread::LockSection;
using fs::thread::Lock;

static const int KOscilloscopeWidth=450;
static const int KOscilloscopeHeight=100;

class AudioOscilloscope
{
public:
	AudioOscilloscope();
	~AudioOscilloscope(void);

	void addAudioBuffer(const void* newBuffer,int newBufferSize,const WAVEFORMATEX& waveFormatex);
	void setAudioBuffer(const void* newBuffer,int newBufferSize,const WAVEFORMATEX& waveFormatex);
	void setAudioBuffer(MutableBuffer* buffer,const WAVEFORMATEX& waveFORMATEX);
	void draw(CWnd* wnd,const CPoint& pos);
	void endDraw();

private:
	void setWaveFormatex(const WAVEFORMATEX& waveFormatex);
	void reset();
	void draw(unsigned char* pBuffer,unsigned int bufferSize,bool onlyDrawBg);
	static void CallRun(LPVOID p);
	bool getAudioBuffer(MutableBuffer* buffer);
	int getSampleValue(unsigned char* pBuffer,int perChannelBytes);
	void drawPoint(int x,int y,int lastX,int lastY);

private:
	CBitmap m_bitmap;
	CDC m_compatibleDC;
	CWnd* m_wnd;
	CPoint m_bitBltPos;

	bool m_draw;
	bool m_isBigBuffer;
	
	MutableBuffer* m_buffer;
	WAVEFORMATEX m_tempWaveFormatex;
	bool m_isInited;

	int m_bytesPerSample;//one sample here include pair of left and right channel sample
	int m_channelCount;
	int m_sampleCount;//one sample here include pair of left and right channel sample,used for bigBuffer
	float m_totalSeconds;//used for bigBuffer

	//draw
	clock_t m_lastClock;
	DWORD m_threadId;
	LockSection m_lockSection;
};

